package com.example.myapplication.Outpatientappointment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ClicktdepartmentBean;
import com.example.myapplication.ShuJu.GetHospitalIdBean;
import com.example.myapplication.apter.ClicktdepartmentApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ClicktdepartmentActivity extends AppCompatActivity {

    private RecyclerView re_clicktdepartment;
    private Request request;
    private Gson gson;
    private OkHttpClient okHttpClient;
    private ClicktdepartmentBean clicktdepartmentBean;
    private Button but_clicktdepartment;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clicktdepartment);
        initview();
        //返回
        bloakActivity();
        //门诊分类
        sendbean();
    }

    private void bloakActivity() {
        but_clicktdepartment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClicktdepartmentActivity.this.finish();
            }
        });
    }

    private void sendbean() {
        request = new Request.Builder()
                .url(TokenBean.IP+"/prod-api/api/hospital/category/list")
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =response.body().string();
                clicktdepartmentBean = gson.fromJson(json, ClicktdepartmentBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ClicktdepartmentApter clicktdepartmentApter = new ClicktdepartmentApter(ClicktdepartmentActivity.this,clicktdepartmentBean.getRows());
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ClicktdepartmentActivity.this);
                        re_clicktdepartment.setAdapter(clicktdepartmentApter);
                        re_clicktdepartment.setLayoutManager(linearLayoutManager);
                    }
                });
            }
        });
    }

    private void initview() {
        re_clicktdepartment = findViewById(R.id.re_clicktdepartment);
        but_clicktdepartment = findViewById(R.id.but_clicktdepartment);

        gson = new Gson();
        okHttpClient = new OkHttpClient();
        handler = new Handler();

    }
}